import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DB {

	private String ipAddress = "10.2.225.199:3306";
	private String dbName = "xengine_test";
	private String user = "root";
	private String password = "admin";
	private String driver = "com.mysql.jdbc.Driver";

	private Connection conn;

	private static DB db = null;

	private DB() {

	}

	public static DB newInstance() {
		if (db == null) {
			db = new DB();
		}
		return db;
	}

	public void connect() {
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection("jdbc:mysql://" + ipAddress
					+ "/" + dbName + "?user=" + user + "&password=" + password);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void close() {
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public App getApp(String appName) {
		App result = null;
		try {
			Statement stat = conn.createStatement();
			ResultSet re = stat
					.executeQuery("select id,name,period from app where name='"
							+ appName + "'");
			if (re.next()) {
				result = new App();
				result.setId(re.getInt(1));
				result.setAppName(re.getString(2));
				result.setPeriod(re.getDouble(3));
			}
			re.close();
			stat.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = null;
		}
		return result;
	}

	public boolean insertApp(String appName) {
		boolean result = false;
		try {
			Statement stat = conn.createStatement();
			ResultSet re = stat
					.executeQuery("select id,name,period from app where name='"
							+ appName + "'");
			re.last();
			if (re.getRow() == 0) {
				stat.executeUpdate("insert into app(name,period) values ('"
						+ appName + "',0.0)");
				result = true;
			} else {
				result = false;
			}
			re.close();
			stat.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = false;
		}
		return result;
	}

	public boolean insertAppHistory(App app, float[] value, int start,
			int stop, int interval, float scale) {
		boolean result = false;
		try {
			Statement stat = conn.createStatement();
			int temp = start;
			for (int i = 0; i < value.length && temp < stop; i++) {
				stat
						.executeUpdate("insert into app_history(app_id,cpu,time) values ("
								+ app.getId()
								+ ","
								+ (value[i] / scale)
								+ ","
								+ temp + ")");
				temp += interval;
			}
			stat.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = false;
		}
		return result;
	}

	public boolean clearAppHistory() {
		boolean result = false;
		try {
			Statement stat = conn.createStatement();
			stat.executeUpdate("delete from app_history");
			result = true;
			stat.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			result = false;
		}
		return result;
	}
}
